﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IESN.Communication
{ 
    //using AxSYSMAC_CS1_Lib;
    using Microsoft.VisualBasic; 
    //using SYSMAC_CS1_Lib;
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Resources;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.CompilerServices;
    using IESN.BusinessCore;
using IESN.Tools;
    using IESN.Common;
    
    public class SysmacAx : IPLC {

        private IContainer components;
        private short DBNo1;
        private short etNodeAddr;
        private short etUnitAddr;
        private short Status_LResetbit;
        private short Status_LRread;
        private short Status_LRwrite;
        private short Status_LSetbit;
        private short Status_NRead1;
        private short Status_NResetbit1;
        private short Status_NSetbit1;
        private short Status_NWrite1;
        private short Status_StatusRead;
        private short Status_StatusWrite;
        private OMRON.Compolet.SYSMAC.SysmacCS _AxSYSMAC_CS11;
        private short Status1; 

        public SysmacAx(short intEtNodeAddr, short intEtUnitAddr)
        { 
            this.Status_NRead1 = 0;
            this.Status_NWrite1 = 0;
            this.Status_NSetbit1 = 0;
            this.Status_NResetbit1 = 0;
            this.Status_StatusRead = 0;
            this.Status_StatusWrite = 0;
            this.Status_LRread = 0;
            this.Status_LRwrite = 0;
            this.Status_LSetbit = 0;
            this.Status_LResetbit = 0;
            this.etNodeAddr = intEtNodeAddr;
            this.etUnitAddr = intEtUnitAddr;
            try
            {
                this.InitializeComponent();
                this._AxSYSMAC_CS11.Active = false;
                this._AxSYSMAC_CS11.NodeAddress = this.etNodeAddr;
                this._AxSYSMAC_CS11.UnitAddress = this.etUnitAddr;
                this._AxSYSMAC_CS11.Active = true;
            }
            catch (Exception exception1)
            {
                LogHelper.Instance.Error(exception1.Message, exception1); 
            }
        }

        public void CloseComm()
        {
            this._AxSYSMAC_CS11.Dispose();
        }
         
        //[DebuggerStepThrough]
        private void InitializeComponent()
        { 
            this._AxSYSMAC_CS11 = new OMRON.Compolet.SYSMAC.SysmacCS(); 
            // 
            // sysmacCS1
            // 
            this._AxSYSMAC_CS11.Active = false;
            this._AxSYSMAC_CS11.DeviceName = "";
            this._AxSYSMAC_CS11.HeartBeatTimer = ((long)(0));
            this._AxSYSMAC_CS11.NetworkAddress = ((short)(0));
            this._AxSYSMAC_CS11.NodeAddress = ((short)(0));
            this._AxSYSMAC_CS11.ReceiveTimeLimit = ((long)(750));
            this._AxSYSMAC_CS11.UnitAddress = ((short)(0));
            this._AxSYSMAC_CS11.UseNameServer = false; 

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="start_dm_address"></param>
        /// <param name="dm_count"></param>
        /// <returns></returns>
        public string NReadWord(short start_dm_address, short dm_count)
        {
            string str2 = "";
            this.Status_NRead = 0;
            try
            {
                //str2 = StringType.FromObject(this._AxSYSMAC_CS11.readArea(SysmacCS1_DataArea_Constants.plcAreaDM, start_dm_address, dm_count, VariantType.String));
                //str2 = StringType.FromObject(this._AxSYSMAC_CS11.readArea(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, start_dm_address, dm_count, VariantType.String));
                //str2 = this._AxSYSMAC_CS11.ReadMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, start_dm_address, dm_count);
                object tempObject = this._AxSYSMAC_CS11.ReadMemory(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, start_dm_address, dm_count, true);
                str2 = StringType.FromObject(tempObject);
                if ((str2.Length > 0) & (str2.Length == (dm_count * 4)))
                {
                    this.Status_NRead = 1;
                    return str2;
                }
                this.Status_NRead = 0;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                str2 = exception1.Message;
                this.Status_NRead = 0;
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        public bool NReadWord(short start_dm_address, short dm_count, ref byte[] value_byte)
        {
            bool flag=false;
            return flag;
        }

        public string NReadWord(short start_dm_address, short dm_count, string DataType)
        {
            string str=string.Empty;
            return str;
        }

        public string NResetBit(short dmaddress, short bitnumber)
        {
            string str = "";
            short[] numArray = new short[5];
            this.Status_NResetbit = 0;
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 1);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 3);
                bitnumber = (short) (bitnumber - 0x30);
            }
            if ((bitnumber >= 0x40) & (bitnumber <= 0x4f))
            {
                dmaddress = (short) (dmaddress + 4);
                bitnumber = (short) (bitnumber - 0x40);
            }
            try
            {
                if (AppConfiguration.EtNodeAddr >= 3)
                {
                    //this._AxSYSMAC_CS11.writeBitArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, bitnumber, 1, "00");
                    this._AxSYSMAC_CS11.WriteMemoryBit(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, bitnumber, new bool[] { false, false });
                }
                else
                {
                    //str = StringType.FromObject(this._AxSYSMAC_CS11.readArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, 1, VariantType.String));
                    str = this._AxSYSMAC_CS11.ReadMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, 1);
                    
                    numArray[1] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 1, 1)));
                    numArray[2] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 2, 1)));
                    numArray[3] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 3, 1)));
                    numArray[4] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 4, 1)));
                    if ((bitnumber >= 0) & (bitnumber <= 3))
                    {
                        numArray[4] = (short) (numArray[4] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) bitnumber)))));
                    }
                    if ((bitnumber >= 4) & (bitnumber <= 7))
                    {
                        numArray[3] = (short) (numArray[3] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 4))))));
                    }
                    if ((bitnumber >= 8) & (bitnumber <= 11))
                    {
                        numArray[2] = (short) (numArray[2] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 8))))));
                    }
                    if ((bitnumber >= 12) & (bitnumber <= 15))
                    {
                        numArray[1] = (short) (numArray[1] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 12))))));
                    }
                    str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                    //this._AxSYSMAC_CS11.writeArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, 1, str);
                    this._AxSYSMAC_CS11.WriteMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, str);
                }
                this.Status_NResetbit = 1;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.Status_NResetbit = 0;
                ProjectData.ClearProjectError();
            }
            return "";
        }

        public string NSetBit(short dmaddress, short bitnumber)
        {
            short[] numArray = new short[5];
            this.Status_NSetbit = 1;
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 1);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 3);
                bitnumber = (short) (bitnumber - 0x30);
            }
            if ((bitnumber >= 0x40) & (bitnumber <= 0x4f))
            {
                dmaddress = (short) (dmaddress + 4);
                bitnumber = (short) (bitnumber - 0x40);
            }
            try
            {
                if (AppConfiguration.EtNodeAddr >= 3)
                {
                    //this._AxSYSMAC_CS11.writeBitArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, bitnumber, 1, "01");
                    this._AxSYSMAC_CS11.WriteMemoryBit(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, bitnumber, new bool[]{false,false});
                }
                else
                {
                    //string str = StringType.FromObject(this._AxSYSMAC_CS11.readArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, 1, VariantType.String));
                    string str = this._AxSYSMAC_CS11.ReadMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, 1);
                    numArray[1] = (short)Math.Round(Conversion.Val("&H" + Strings.Mid(str, 1, 1)));
                    numArray[2] = (short)Math.Round(Conversion.Val("&H" + Strings.Mid(str, 2, 1)));
                    numArray[3] = (short)Math.Round(Conversion.Val("&H" + Strings.Mid(str, 3, 1)));
                    numArray[4] = (short)Math.Round(Conversion.Val("&H" + Strings.Mid(str, 4, 1)));
                    if ((bitnumber >= 0) & (bitnumber <= 3))
                    {
                        numArray[4] = (short) (numArray[4] | ((long) Math.Round(Math.Pow(2.0, (double) bitnumber))));
                    }
                    if ((bitnumber >= 4) & (bitnumber <= 7))
                    {
                        numArray[3] = (short) (numArray[3] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 4)))));
                    }
                    if ((bitnumber >= 8) & (bitnumber <= 11))
                    {
                        numArray[2] = (short) (numArray[2] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 8)))));
                    }
                    if ((bitnumber >= 12) & (bitnumber <= 15))
                    {
                        numArray[1] = (short) (numArray[1] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 12)))));
                    }
                    str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                    //this._AxSYSMAC_CS11.writeArea(SysmacCS1_DataArea_Constants.plcAreaDM, dmaddress, 1, str);
                    this._AxSYSMAC_CS11.WriteMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, dmaddress, str);
                }
                this.Status_NSetbit = 1;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.Status_NSetbit = 0;
                ProjectData.ClearProjectError();
            }
            return "";
        }

        public bool NWriteWord(short start_dm_address, short dm_count, byte[] SendArry)
        {
            bool flag=false;
            return flag;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="start_dm_address"></param>
        /// <param name="dm_count"></param>
        /// <param name="Indata"></param>
        /// <returns></returns>
        public string NWriteWord(short start_dm_address, short dm_count, string Indata)
        {
            short num=0;
            string writeData = Indata;
            if (dm_count == 0)
            {
                if ((writeData.Length % 4) == 0)
                {
                    //num = (short)Math.Round(((double)(((double)writeData.Length) / 4.0)));//ou 
                    num = (short) Math.Round(Conversion.Int((double) (((double) writeData.Length) / 4.0)));
                }
                else
                {
                    //num = (short)Math.Round((double)((double)(((double)writeData.Length) / 4.0) + 1.0));//ou 
                    num = (short) Math.Round((double) (Conversion.Int((double) (((double) writeData.Length) / 4.0)) + 1.0));
                }
            }
            else
            {
                num = dm_count;
            }
            this.Status_NWrite = 0;
            try
            {
                //this._AxSYSMAC_CS11.writeArea(SysmacCS1_DataArea_Constants.plcAreaDM, start_dm_address, num, writeData);
                this._AxSYSMAC_CS11.WriteMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.DM, start_dm_address, writeData);
                this.Status_NWrite = 1;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.Status_NWrite = 0;
                ProjectData.ClearProjectError();
            }
            return "";
        }


        public string NWriteWord(short start_dm_address, short dm_count, string Indata, string DataType)
        {
            string str=string.Empty;
            return str;
        }

        public object writeBitArea(short startOffset, short startBitOffset, short bitSize, string WriteData)
        {
            object obj2=null;
            try
            {
                //this._AxSYSMAC_CS11.writeBitArea(SysmacCS1_DataArea_Constants.plcAreaCIO, startOffset, startBitOffset, bitSize, WriteData);
                this._AxSYSMAC_CS11.WriteMemoryString(OMRON.Compolet.SYSMAC.SysmacCSBase.MemoryTypes.CIO, startOffset, WriteData);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ProjectData.ClearProjectError();
            }
            return obj2;
        }

        //internal virtual AxSYSMAC_CS1 AxSYSMAC_CS11
        //{
        //    get
        //    {
        //        return this._AxSYSMAC_CS11;
        //    }
        //    [MethodImpl(MethodImplOptions.Synchronized)]
        //    set
        //    {
        //        if (this._AxSYSMAC_CS11 != null)
        //        {
        //        }
        //        this._AxSYSMAC_CS11 = value;
        //        if (this._AxSYSMAC_CS11 != null)
        //        {
        //        }
        //    }
        //}

        public short DBNo
        {
            get
            {
                return this.DBNo1;
            }
            set
            {
                this.DBNo1 = value;
            }
        }

        public short Status
        {
            get
            {
                return this.Status1;
            }
            set
            {
                this.Status1 = value;
            }
        }

        public short Status_NRead
        {
            get
            {
                return this.Status_NRead1;
            }
            set
            {
                this.Status_NRead1 = value;
            }
        }

        public short Status_NResetbit
        {
            get
            {
                return this.Status_NResetbit1;
            }
            set
            {
                this.Status_NResetbit1 = value;
            }
        }

        public short Status_NSetbit
        {
            get
            {
                return this.Status_NSetbit1;
            }
            set
            {
                this.Status_NSetbit1 = value;
            }
        }

        public short Status_NWrite
        {
            get
            {
                return this.Status_NWrite1;
            }
            set
            {
                this.Status_NWrite1 = value;
            }
        }

      
        }
}
